public class BinarySearchTree {
     static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root;
    /**
     * 判断val是否存在
     */
    public boolean search(int val) {
        TreeNode cur = root;
        //1.遍历搜索树
        while(cur != null) {
            //2.与根进行比较
            if(cur.val < val) {
                cur = cur.right;
            }else if(cur.val > val) {
                cur = cur.left;
            }else {
                return true;
            }
        }
        return false;
    }
    /**
     * 插入数据
     */
    public void insert(int val) {
        //1.判断是否已经存在
        if(root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        //2.循环找到val插入的点,并使用parent记录到它的前一个节点
        while(cur != null) {
            if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else {
                return;
            }
        }
        //循环结束,判断parent与节点值比较
        TreeNode node = new TreeNode(val);
        if(parent.val > val) {
            parent.left = node;
        }else {
            parent.right = node;
        }
    }
    /**
     * 删除节点
     */
    public void remove(int val) {
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null) {
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else {
                removeNode(cur,parent);
            }
        }
    }
    private void removeNode(TreeNode cur,TreeNode parent) {
        //1.cur左为空
        if(cur.left == null) {
            //1.1cur为root
            if(cur == root) {
                root = cur.right;
                //1.2cur== parent.left
            }else if(cur == parent.left) {
                parent.left = cur.right;
            }else {//1.3cur == parent.right
                parent.right = cur.right;
            }
        }else if(cur.right == null) {//2.cur右未空
            if(cur == root) {
                root = cur.left;
            }else if(cur == parent.left) {//等于左
                parent.left = cur.left;
            }else {//等于右
                parent.right = cur.left;
            }
        }else {//3.cur左右不为空
            //3.1找到cur左子树中的最大值,或右子树的最小值,进行替换,即覆盖掉cur值即可
            TreeNode t = cur.right;//用来代替cur的节点,此处是找cur右子树最小值,所以=cur.right
            TreeNode tp = cur;//替罪羊的父亲节点
            while(t.left != null) {
                //循环找最小值,最小值在树的左节点
                tp = t;
                t = t.left;
            }
            //循环结束,替换
            cur.val = t.val;
            //如果t存在右节点
            if(tp.left == t) {
                tp.left = t.right;
            }else {//不存在右节点
                tp.right = t.right;
            }
        }
    }
}
